package com.xsy.android.compose.wandroid.ui.page.login.view

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.tooling.preview.Preview
import com.xsy.android.compose.wandroid.R
import com.xsy.android.compose.wandroid.ui.view.edit.PasswordState
import com.xsy.android.compose.wandroid.ui.view.edit.TextFieldState

@Composable
fun Password(
    modifier: Modifier = Modifier,
    label: String,
    passwordState: TextFieldState,
    imeAction: ImeAction = ImeAction.Done,
    onImeAction: () -> Unit = {}
) {
    val showPassword = remember {
        mutableStateOf(false)
    }

    OutlinedTextField(value = passwordState.text, onValueChange = {
        passwordState.text = it
        passwordState.enableShowError()
    },
        modifier = modifier
            .fillMaxWidth()
            .onFocusChanged { focusState ->
                val focused = focusState.isFocused
                passwordState.onFocusChange(focused)
                if (!focused) {
                    passwordState.enableShowError()
                }
            },
        textStyle = MaterialTheme.typography.bodyMedium,
        label = {
            Text(
                text = label,
                style = MaterialTheme.typography.bodyMedium
            )
        },
        trailingIcon = {
            if (showPassword.value) {
                IconButton(onClick = { showPassword.value = false }) {
                    Icon(
                        painter = painterResource(id = R.drawable.ic_baseline_visibility_24),
                        contentDescription = stringResource(id = R.string.hide_password)
                    )
                }
            } else {
                IconButton(onClick = { showPassword.value = true }) {
                    Icon(
                        painter = painterResource(id = R.drawable.ic_baseline_visibility_off_24),
                        contentDescription = stringResource(id = R.string.show_password)
                    )
                }
            }
        },
        visualTransformation = if (showPassword.value) {
            VisualTransformation.None
        } else {
            PasswordVisualTransformation()
        },
        isError = passwordState.showErrors(),
        keyboardOptions = KeyboardOptions.Default.copy(imeAction = imeAction),
        keyboardActions = KeyboardActions(
            onDone = {
                onImeAction()
            }
        )
    )
    passwordState.getError()?.let { error -> TextFieldError(textError = error) }
}


@Preview(showBackground = true)
@Composable
private fun PreviewPassword() {
    val focusRequester = remember {
        FocusRequester()
    }
    val passwordState = remember {
        PasswordState()
    }
    Column {
        Password(
            modifier = Modifier.focusRequester(focusRequester),
            label = stringResource(id = R.string.password),
            passwordState = passwordState,
            onImeAction = {

            }
        )
    }

}